package me.khrystal.rxnews.module.photo.model;

import java.util.List;

import me.khrystal.rxnews.bean.SinaPhotoDetail;
import me.khrystal.rxnews.callback.RequestCallback;
import me.khrystal.rxnews.http.HostType;
import me.khrystal.rxnews.http.manager.RetrofitManager;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/4/29
 * update time:
 * email: 723526676@qq.com
 */
public class IPhotoDetailInteractorImpl
        implements IPhotoDetailInteractor<SinaPhotoDetail>{
    @Override
    public Subscription requestPhotoDetail(final RequestCallback<SinaPhotoDetail> callback, String id) {
        return RetrofitManager.getInstance(HostType.SINA_NEWS_PHOTO).getSinaPhotoDetailObservable(id)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        callback.beforeRequest();
                    }
                }).subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<SinaPhotoDetail>() {
                    @Override
                    public void onCompleted() {
                        callback.requestComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.requestError(e + "\n" + e.getLocalizedMessage());
                    }

                    @Override
                    public void onNext(SinaPhotoDetail sinaPhotoDetail) {
                        callback.requestSuccess(sinaPhotoDetail);
                    }
                });

    }
}
